/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.risk.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.risk.domain.PointManagement;
import com.koron.risk.service.api.PointManagementService;
import com.koron.risk.util.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = "pointManagement", description = "坐标管理")
@RestController
@RequestMapping("/pointManagement")
public class PointManagementController {

    @Autowired
    private PointManagementService service;

    @ApiOperation(value = "坐标保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "新增不传，修改传" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "类型" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sortNo", value = "排序" , paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pointX", value = "x坐标" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pointY", value = "y坐标" , paramType = "query", dataType = "String")})
    @RequestMapping(value = "/sava.htm", method = {RequestMethod.POST})
    public MessageBean<?> sava(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, PointManagement point) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        point.setUpdateTime(new Date());
        point.setUpdateUser(user.getCode());
        point.setUpdateUserName(user.getName());
        MessageBean<?> msg = service.sava(user, point);
        return msg;
    }

    @ApiOperation(value = "获取坐标详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id" , paramType = "query", dataType = "String")})
    @RequestMapping(value = "/details.htm", method = {RequestMethod.GET})
    public MessageBean<PointManagement> details(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<PointManagement> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        MessageBean<PointManagement> msg = service.details(user, id);
        return msg;
    }


    @ApiOperation(value = "删除坐标信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id" , paramType = "query", dataType = "String")})
    @RequestMapping(value = "/delete.htm", method = {RequestMethod.POST})
    public MessageBean<Integer> delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<Integer> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        MessageBean<Integer> msg = service.delete(id);
        return msg;
    }

    @ApiOperation(value = "获取坐标列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "id" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "类型" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "operationType", value = "1.公司，2水厂" , paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "page", value = "当前页" , paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , paramType = "query", dataType = "int")})
    @RequestMapping(value = "/list.htm", method = {RequestMethod.GET})
    public MessageBean<DataBean> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode, String name, String type, Integer operationType, String title, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<DataBean> msg = new MessageBean<>();
            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
            return msg;
        }
        MessageBean<DataBean> msg = service.list(user, corpCode, name, title, type, operationType, page, pageSize);
        return msg;
    }


    @ApiOperation("获取坐标点所属的隐患统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code" , dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/dangerAnalysis.htm", method = RequestMethod.GET)
    public MessageBean<Map> dangerAnalysis(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                           @RequestParam(required = false) final String corpCode) {
        if (null == user) {
            return MessageBean.create(10000, "未登录", null);
        }
        return ADOConnection.runTask(factory -> service.dangerAnalysis(factory, corpCode), MessageBean.class);
    }


    @ApiOperation(value = "获取单位下所有坐标点信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code,不传值默认集团" , paramType = "query", dataType = "String")})
    @RequestMapping(value = "/getAll.htm", method = {RequestMethod.GET})
    public MessageBean<List> getAll(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode) {
//        if (null == user) {
//            MessageBean<DataBean> msg = new MessageBean<>();
//            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
//            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
//            return msg;
//        }
        MessageBean<List> msg = service.getAll(user, corpCode);
        return msg;
    }


    @ApiOperation(value = "获取所有坐标点信息树形结构")
    @RequestMapping(value = "/tree.htm", method = {RequestMethod.GET})
    public MessageBean<PointManagement> tree(HttpServletResponse response, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
//        if (null == user) {
//            MessageBean<DataBean> msg = new MessageBean<>();
//            msg.setCode(BusinessCode.MESSAGE_CODE_ACTION_EXCEPTION);
//            msg.setDescription(BusinessCode.MESSAGE_DESCRIPTION_ACTION_EXCEPTION);
//            return msg;
//        }
        MessageBean<PointManagement> msg = service.tree(user);
        response.setContentType("application/json;charset=UTF-8");
        return msg;
    }


    @ApiOperation("获取坐标点所属的隐患统计数据,当前年和当前月")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/dangerAnalysisWithDate.htm", method = RequestMethod.GET)
    public MessageBean<Map> dangerAnalysisWithDate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                                   @RequestParam(required = false) final String corpCode) {
//        if (null == user) {
//            return MessageBean.create(10000, "未登录", null);
//        }
        return ADOConnection.runTask(factory -> service.dangerAnalysisWithDate(factory, corpCode), MessageBean.class);
    }

    @ApiOperation("获取坐标点所属的安全检查统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "年" , dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/safetyCheckAnalysis.htm", method = RequestMethod.GET)
    public MessageBean<List<Map>> safetyCheckAnalysis(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                                      @RequestParam(required = false) final String corpCode,
                                                      @RequestParam(required = false) Integer year) {
//        if (null == user) {
//            return MessageBean.create(10000, "未登录", null);
//        }
        return ADOConnection.runTask(factory -> service.safetyCheckAnalysis(factory, corpCode, year), MessageBean.class);
    }

    @ApiOperation("获取坐标点所属的危险作业统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "年" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "operationTicketType", value = "作业票类型（1临时用电作业，2有限空间作业，3起重吊装作业，4动火作业，5破土作业，6高空作业，7抽堵盲板作业，8设备检修作业，9断路安全作业）" , dataType = "String", paramType = "query")

    })
    @RequestMapping(value = "/dangerousOperationAnalysis.htm", method = RequestMethod.GET)
    public MessageBean<Map> dangerousOperationAnalysis(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                                       @RequestParam(required = false) final String corpCode,
                                                       @RequestParam(required = false) final String operationTicketType,
                                                       @RequestParam(required = false) Integer year) {
//        if (null == user) {
//            return MessageBean.create(10000, "未登录", null);
//        }
        return ADOConnection.runTask(factory -> service.dangerousOperationAnalysis(factory, corpCode, operationTicketType, year), MessageBean.class);
    }

    @ApiOperation("获取坐标点所属的安全演练统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "time", value = "查询月份" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "年" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "mouth", value = "月" , dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/safetyDrillAnalysis.htm", method = RequestMethod.GET)
    public MessageBean<Map> safetyDrillAnalysis(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                                @RequestParam(required = false) String corpCode,
                                                @RequestParam(required = false) String time,
                                                @RequestParam(required = false) Integer year,
                                                @RequestParam(required = false) Integer mouth) {
//        if (null == user) {
//            return MessageBean.create(10000, "未登录", null);
//        }
        return ADOConnection.runTask(factory -> service.safetyDrillAnalysis(factory, corpCode, time, year, mouth), MessageBean.class);
    }

    @ApiOperation("获取坐标点所属的安全培训统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "time", value = "查询月份" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "年" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "mouth", value = "月" , dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/safetyTrainAnalysis.htm", method = RequestMethod.GET)
    public MessageBean<Map> safetyTrainAnalysis(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                                @RequestParam(required = false) String corpCode,
                                                @RequestParam(required = false) String time,
                                                @RequestParam(required = false) Integer year,
                                                @RequestParam(required = false) Integer mouth) {
//        if (null == user) {
//            return MessageBean.create(10000, "未登录", null);
//        }
        return ADOConnection.runTask(factory -> service.safetyTrainAnalysis(factory, corpCode, time, year, mouth), MessageBean.class);
    }

    @ApiOperation("获取坐标点所属的危险源统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/getHazarsSourceAnalysis.htm", method = RequestMethod.GET)
    public MessageBean<Map> getHazarsSourceAnalysis(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                                    @RequestParam(required = false) String corpCode) {
//        if (null == user) {
//            return MessageBean.create(10000, "未登录", null);
//        }
        return ADOConnection.runTask(factory -> service.getHazarsSourceAnalysis(factory, corpCode), MessageBean.class);
    }

}
